package com.echat.business.blog.controller;

import com.echat.business.blog.model.BlogType;
import com.echat.business.blog.service.IBlogTypeService;
import com.echat.business.home.model.SysUser;
import com.echat.config.common.RequestHolder;
import com.echat.core.constant.response.ResponseData;
import com.echat.core.utils.StringUtils;
import com.echat.core.utils.ValueUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author chentl
 * @version V1.0
 * @Project BlogController
 * @Title BlogController.java
 * @Description blog 控制器
 * @Package com.echat.business.blog.controller
 * @date 2019/9/5 18:03
 * @Copyright: 上海顺益信息科技有限公司 All rights reserved.
 */
@RestController
@Slf4j
@RequestMapping("/blogType")
public class BlogTypeController {

    @Autowired
    private IBlogTypeService blogTypeService;

    @RequestMapping(value = "/list")
    public ResponseData getBlogList() {
        List<Map<String, Object>> list = blogTypeService.getBloyTypeList();
        return new ResponseData(true, 200, "成功", list);
    }


    @RequestMapping(value = "/typeList")
    public ResponseData getTypeList() {
        SysUser loginUser = RequestHolder.getCurrentUser();
        List<BlogType> list = blogTypeService.getBlogTypeList(loginUser.getId());
        return new ResponseData(true, 200, "获取博客类型列表成功", list);
    }

    @RequestMapping(value = "/typeIndex")
    public ResponseData getTypeIndex(String blogType) {
        SysUser loginUser = RequestHolder.getCurrentUser();
        if (StringUtils.isBlank(blogType)) {
            return ResponseData.error("博客类型id为空");
        }
        BlogType data = blogTypeService.getBlogTypeIndex(loginUser.getId(), ValueUtils.parseInteger(blogType));
        return new ResponseData(true, 200, "获取博客类型索引成功", data);
    }

    @RequestMapping(value = "/add")
    public ResponseData addBlogType(String sortName) {
        if (StringUtils.isBlank(sortName)) {
            return ResponseData.error("参数中，sortName为空");
        }
        SysUser loginUser = RequestHolder.getCurrentUser();
        Integer userId = loginUser.getId();
        //重名校验
        if (blogTypeService.validRepeat(sortName, userId)) {
            return ResponseData.error("该分类已经存在了←.←");
        }
        boolean result = blogTypeService.insertBlogType(sortName, userId);
        if (result) {
            return ResponseData.success(200, "添加分类成功", null);
        }
        return ResponseData.error("添加分类失败");
    }


    @RequestMapping(value = "/edit")
    public ResponseData edit(BlogType blogType) {
        //重名校验
        if (blogTypeService.validRepeat(blogType.getName(), blogType.getId())) {
            return ResponseData.error("该分类已经存在了←.←");
        }
        boolean result = blogTypeService.updateById(blogType);
        if (result) {
            return ResponseData.success(200, "分类名称修改成功", null);
        } else {
            return ResponseData.error(500, "分类修改失败", null);
        }
    }

    @RequestMapping(value = "/delete")
    public ResponseData delete(BlogType blogType) {
        boolean result = blogTypeService.deleteById(blogType);
        if (result) {
            return ResponseData.success(200, "分类名称删除成功", null);
        } else {
            return ResponseData.error(500, "分类删除失败", null);
        }
    }


}
